package cn.ml.duoXianCheng.demos;

import java.util.ArrayList;
import java.util.List;

public class ZhengManTou {
    public static List<String> manTou = new ArrayList<>();
    public static void main(String[] args) {
        /*
        1 2  3  4
        */
        new Zheng().start();
        new Chi("张三三").start();
        new Chi("李四四").start();
        new Chi("王五五").start();
    }
    //蒸馒头
    static class Zheng extends Thread{
        @Override
        public void run() {

            while (true) {
                synchronized ("a") {  //蒸馒头进入代码块，别人不能吃
                    for (int i = 0; i < 30; i++) {
                        ZhengManTou.manTou.add("弟" + (i + 1) + "个馒头");
                        System.out.println(Thread.currentThread().getName()+"已做好" + ZhengManTou.manTou.get(i));
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println(Thread.currentThread().getName()+"馒头做好了，我喊醒所有人。。。");
                    "a".notifyAll();
                }
                synchronized ("b"){
                    try {
                        System.out.println(Thread.currentThread().getName()+"蒸馒头的就去睡了");
                        "b".wait();
                        Thread.sleep(3000);//被唤醒后等3秒才开始做
                        System.out.println(Thread.currentThread().getName()+"没馒头了，开始做。。。");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    //吃馒头
    static class Chi extends Thread{
        private String name;
        public Chi(String name){
            this.name = name;
        }
        @Override
        public void run() {
            while (true) {
                synchronized ("a") {
                    if (ZhengManTou.manTou.size() > 0) {  //如果有馒头就吃馒头
                        String m = ZhengManTou.manTou.remove(0);//馒头中 减去第一个
                        System.out.println(Thread.currentThread().getName()+this.name + "吃掉了" + m);
                        //System.out.println(m);
                    }else {  //如果没馒头就等待
                        try {
                            synchronized ("b"){
                                System.out.println(Thread.currentThread().getName()+this.name+"发现没有馒头，叫蒸馒头的。。。");
                                "b".notify();
                            }
                            System.out.println(Thread.currentThread().getName()+this.name+"睡了");
                            "a".wait();
                            System.out.println(Thread.currentThread().getName()+this.name+"被叫醒了，说明有馒头了");
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
                //消化过程不能再锁里
                int rd = (int) (Math.random() * 1000);   //随机生成2000的随机数
                try {
                    Thread.sleep(rd);   //吃的过程消化rd毫秒
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}